//
//  MovieListViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "MovieDetailViewController.h"
#import "MBProgressHUD.h"

@interface MovieListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, retain) NSMutableArray *movieArray;


@end

@implementation MovieListViewController

- (void)dealloc
{
    [_movieArray release];
    [_myTableView release];
    [_hub release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, 375, 667)];
    
    self.myTableView.delegate = self;
    
    self.myTableView.dataSource = self;
    
    [self.view addSubview:self.myTableView];
    
    [_myTableView release];
    
    self.navigationItem.title = @"电影";
    
    [self.myTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"movie"];
    
    self.movieArray = [NSMutableArray array];
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";

    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中....";
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        NSString *reason = [dic objectForKey:@"reason"];
        
        NSMutableArray *array = [dic objectForKey:@"result"];
        
        if ([reason isEqualToString:@"success"]) {
            
            for (NSDictionary *dic in array) {
                
                Movie *movie = [[Movie alloc] init];
                
                [movie setValuesForKeysWithDictionary:dic];
                
                [self.movieArray addObject:movie];
                
                [movie release];
            }
            
            [self.myTableView reloadData];
            [self.hub hide:YES];
            
        }else{
            
            NSLog(@"请求失败");
            
        }
        
    }];

    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.movieArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellaa = @"movie";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellaa];
    
    Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.myMovie = mov;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc] init];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    movieDVC.movieID = movie.movieId;
    
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    [movieDVC release];
    
    
}

- (void)buttonAction:(UIBarButtonItem *)btn
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

